package com.zjljr.mqProvider.controller;

import com.zjljr.common.message.PraiseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("sendMsg")
public class SendMgController {

    private final static String  BINDING_NAME = "praise-out";

    @Autowired
    StreamBridge streamBridge;

    @GetMapping("/rocketmq")
    public boolean praise(@RequestParam Integer itemId){
        PraiseMessage msg = new PraiseMessage();
        msg.setItemId(itemId);
        Message<PraiseMessage> message = MessageBuilder.withPayload(msg).build();
        // 发送消息时需要cmd先启动 mqnamesrv：【start mqnamesrv.cmd】，
        // 然后使用mqbroker连接到mqnamesrv对应的服务ip： 【start mqbroker.cmd -n 127.0.0.1:9876】
        return streamBridge.send(BINDING_NAME, message);
    }
}
